package cn.t.repository.impl.user;

import cn.t.converter.user.CustomerIndividualConverter;
import cn.t.core.mode.YesNoEnum;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.CustomerIndividualDO;
import cn.t.persistence.entity.user.CustomerIndividualPO;
import cn.t.persistence.mapper.user.CustomerIndividualMapper;
import cn.t.repository.user.CustomerIndividualRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 单位信息表数据仓库
 */
@Slf4j
@Repository
@AllArgsConstructor
public class CustomerIndividualRepositoryImpl extends RepositoryImpl<CustomerIndividualMapper, CustomerIndividualPO, CustomerIndividualConverter, CustomerIndividualDO> implements CustomerIndividualRepository {


    @Override
    public List<CustomerIndividualDO> findByModel(CustomerIndividualDO customerIndividualDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = StringUtils.hasText(field) ? " order by " + field + " " + direction + " limit " + start + "," + pageSize : " limit " + start + "," + pageSize;

        List<CustomerIndividualPO> list = getWrapper(customerIndividualDO).last(lastSql).list();
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private LambdaQueryChainWrapper<CustomerIndividualPO> getWrapper(CustomerIndividualDO customerIndividualDO) {
        LambdaQueryChainWrapper<CustomerIndividualPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(customerIndividualDO.getId())) {
            wrapper.eq(CustomerIndividualPO::getId, customerIndividualDO.getId());
        }
        if (!ObjectUtils.isEmpty(customerIndividualDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(customerIndividualDO.getGmtCreatedEnd())) {
            wrapper.between(CustomerIndividualPO::getGmtCreated, customerIndividualDO.getGmtCreatedStart(), customerIndividualDO.getGmtCreatedEnd());
        }
        if (!ObjectUtils.isEmpty(customerIndividualDO.getGmtModifiedStart()) && !ObjectUtils.isEmpty(customerIndividualDO.getGmtModifiedEnd())) {
            wrapper.between(CustomerIndividualPO::getGmtModified, customerIndividualDO.getGmtModifiedStart(), customerIndividualDO.getGmtModifiedEnd());
        }
        if (!ObjectUtils.isEmpty(customerIndividualDO.getCustomerId())) {
            wrapper.eq(CustomerIndividualPO::getCustomerId, customerIndividualDO.getCustomerId());
        }
        if (!ObjectUtils.isEmpty(customerIndividualDO.getCustomerName())) {
            wrapper.eq(CustomerIndividualPO::getCustomerName, customerIndividualDO.getCustomerName());
        }

        if (!ObjectUtils.isEmpty(customerIndividualDO.getStatus())) {
            wrapper.eq(CustomerIndividualPO::getStatus, customerIndividualDO.getStatus());
        }
        return wrapper;
    }

    @Override
    public long count(CustomerIndividualDO customerIndividualDO) {
        return getWrapper(customerIndividualDO).count();
    }

    @Override
    public boolean deleteCustomer(Long individualId) {
        return lambdaUpdate().set(CustomerIndividualPO::getStatus, String.valueOf(BaseStatusEnum.DISABLE.getCode()))
                .set(CustomerIndividualPO::getIsDeleted, String.valueOf(YesNoEnum.TRUE.getCode()))
                .set(CustomerIndividualPO::getGmtModified, LocalDateTime.now()).eq(CustomerIndividualPO::getId, individualId).update();
    }
}
